package trunk.model.calendar;

import java.awt.Color;
import java.io.Serializable;

import java.util.ArrayList;
import trunk.model.BancoDeConvidados;
import trunk.model.Cliente;
import trunk.model.TipoStatusEvento;

@SuppressWarnings("serial")
public class Event implements EventInstance, Serializable {
	private static int IDgenerator;
	private TipoStatusEvento status;
	private ArrayList<String> anexos;
	private int ID;
	private String title, description, location;
	private int Y, M, D, h, m, s;
	private Cliente clienteProprietario;
	private BancoDeConvidados clientesParticipantes;
	private boolean hasTime, allDay, hasAttachments;
	private Color fg, bg, border;
	private Color[] colors = { Color.blue, Color.red, Color.orange, Color.pink,
	    Color.gray, Color.green, Color.yellow, Color.cyan, Color.magenta };

	public Event(String title, String description, int Y, int M, int D) {
		this ( title, description,null, Y, M, D, 0, 0, 0, false, false, null, null, false, null);
	}

	public Event(String title, String description, int Y, int M, int D, int h,
	    int m, int s) {
		this ( title, description, null, Y, M, D, h, m, s, true, false, null, null, false, null);
	}

	public Event(String title, String description, String location, int Y, int M, int D, int h,
	    int m, int s, boolean hasTime, boolean allDay, 
	    Cliente proprietario, BancoDeConvidados convidados, boolean hasAttachments, ArrayList<String> anexos ) {
		this.ID = ++IDgenerator;
		this.status = TipoStatusEvento.PENDENTE;
		this.title = title;
		this.description = description;
		this.location = location;
		this.Y = Y;
		this.M = M;
		this.D = D;
		this.h = h;
		this.m = m;
		this.s = s;
		this.hasTime = hasTime;
		this.allDay = allDay;
		this.clienteProprietario = proprietario;
		this.clientesParticipantes = convidados;
		// this.fg = new Color ( D * 8 % 256, h * 10 % 256, m * 4 % 256 );
		this.bg = colors[ ( Y + M + D + h + m + s ) % colors.length];
		this.fg = new Color ( this.bg.getRed () / 2, this.bg.getGreen () / 2,
		    this.bg.getBlue () / 2 );
		this.border = this.fg;
		this.hasAttachments = hasAttachments;
	}
	
	public Cliente getClienteProprietario() {
		return clienteProprietario;
	}

	public String getTitle () {
		return title;
	}

	public String getDescription () {
		return description;
	}

	public boolean isAllDay () {
		return allDay;
	}

	public boolean hasTime () {
		return hasTime;
	}

	public int getYear () {
		return Y;
	}

	public int getMonth () {
		return M;
	}

	public int getDayOfMonth () {
		return D;
	}

	public int getHour () {
		return h;
	}

	public int getMinute () {
		return m;
	}

	public int getSecond () {
		return s;
	}

	public boolean hasDuration () {
		return false;
	}

	public String getLocation () {
		return location;
	}

	public int getDurationSeconds () {
		return 0;
	}

	public Color getForegroundColor () {
		return fg;
	}

	public Color getBackgroundColor () {
		return bg;
	}

	public Color getBorderColor () {
		return border;
	}

	public BancoDeConvidados getClientesParticipantes() {
		return clientesParticipantes;
	}
        
          public ArrayList<String> getAnexos() {
          return anexos;
    }

	/** Implement the Comparable interface so events can be sorted */
	public int compareTo ( Object o ) {
		EventInstance e2 = (EventInstance) o;
		if ( this.getYear () < e2.getYear () )
			return -1;
		else if ( this.getYear () > e2.getYear () )
			return 1;
		if ( this.getMonth () < e2.getMonth () )
			return -1;
		else if ( this.getMonth () > e2.getMonth () )
			return 1;
		if ( this.getDayOfMonth () < e2.getDayOfMonth () )
			return -1;
		else if ( this.getDayOfMonth () > e2.getDayOfMonth () )
			return 1;
		if ( !this.hasTime && e2.hasTime () )
			return -1;
		else if ( this.hasTime () && !e2.hasTime () )
			return 1;
		else if ( !this.hasTime && !e2.hasTime () )
			return 0;
		if ( this.isAllDay () && !e2.isAllDay () )
			return -1;
		if ( !this.isAllDay () && e2.isAllDay () )
			return 1;
		if ( this.isAllDay () && e2.isAllDay () )
			return 0;
		// both events have a time
		if ( this.getHour () < e2.getHour () )
			return -1;
		else if ( this.getHour () > e2.getHour () )
			return 1;
		if ( this.getMinute () < e2.getMinute () )
			return -1;
		else if ( this.getMinute () > e2.getMinute () )
			return 1;
		if ( this.getSecond () < e2.getSecond () )
			return -1;
		else if ( this.getSecond () > e2.getSecond () )
			return 1;

		return 0;
	}

	public boolean isHasAttachments() {
		return hasAttachments;
	}

	@Override
	public int getID() {
		return ID;
	}

	public void setHasAttachments(boolean hasAttachments) {
		this.hasAttachments = hasAttachments;
	}

	@Override
	public TipoStatusEvento getStatus() {
		return status;
	}

	@Override
	public void setStatus(TipoStatusEvento status) {
		this.status = status;
	}

	@Override
	public boolean hasAttachment() {
		return hasAttachments;
	}

	@Override
	public Cliente getOwner() {
		return clienteProprietario;
	}

	@Override
	public BancoDeConvidados getInviteds() {
		return clientesParticipantes;
	}
}
